package com.laikan.reader.bookreader.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.WindowManager;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.bookreader.activity.ReaderDetailActivity;
import com.laikan.reader.core.utils.PublicUtil;

/**
 * Created by wx on 16/8/22.
 */
public class ReaderDetailUtil {
    public static final long INTERVAL = 1000L; // 防止连续点击的时间间隔
    private static long preLastClickTime = 0L; // 上一次点击的时间
    private static long nexLastClickTime = 0L; // 上一次点击的时间
    /**
     * 控制【上一章按钮】点击频率，即控制点击间隔时间。
     * @return
     */
    public static boolean preChapterButton() {
        long time = System.currentTimeMillis();
        if ((time - preLastClickTime) > INTERVAL) {
            preLastClickTime = time;
            return true;
        }
        return false;
    }

    /**
     * 控制【下一章按钮】点击频率，即控制点击间隔时间。
     */
    public static boolean nexChapterButton() {
        long time = System.currentTimeMillis();
        if ((time - nexLastClickTime) > INTERVAL) {
            nexLastClickTime = time;
            return true;
        }
        return false;
    }

    /**
     * 设置为全屏显示
     */
    public static void setFullScreen(Context context) {
        Activity thisContext = (Activity) context;
        thisContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        thisContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        WindowManager.LayoutParams attr = thisContext.getWindow().getAttributes();
        attr.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        thisContext.getWindow().setAttributes(attr);
    }

    /**
     * 退出全屏显示
     */
    public static void cancelFullScreen(Context context) {
        Activity thisContext = (Activity) context;
        thisContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        thisContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        thisContext.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        WindowManager.LayoutParams attr = thisContext.getWindow().getAttributes();
        attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        thisContext.getWindow().setAttributes(attr);
    }


    /**
     * 初始化代码设置图片颜色
     */
    public static void initBmp(ReaderDetailActivity activity) {
        Context context = (Context)activity;
        activity.back_b = PublicUtil.readBitMap(context, R.drawable.lk_back_gray);
        activity.buy_b = PublicUtil.readBitMap(context, R.drawable.lk_buy);
        activity.huiying_b = PublicUtil.readBitMap(context, R.drawable.lk_reader_pinglun);
        activity.dashang_b = PublicUtil.readBitMap(context, R.drawable.lk_dashang);
        activity.more_b = PublicUtil.readBitMap(context, R.drawable.lk_more);
        activity.read_xiao = PublicUtil.readBitMap(context, R.drawable.njianju_xiao);
        activity.read_zhong = PublicUtil.readBitMap(context, R.drawable.njianju_zhong);
        activity.read_da = PublicUtil.readBitMap(context, R.drawable.njianju_da);
        activity.read_border = PublicUtil.readBitMap(context, R.drawable.read_menu_border);
        activity.read_mulu = PublicUtil.readBitMap(context, R.drawable.lk_reader_mulu);
        activity.read_font = PublicUtil.readBitMap(context, R.drawable.lk_reader_font);
        activity.read_mode_r = PublicUtil.readBitMap(context, R.drawable.lk_reader_mode);
        activity.read_mode_n = PublicUtil.readBitMap(context, R.drawable.lk_reader_light_more);
        activity.drawable_xiao = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#666666")));
        activity.drawable_zhong = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#666666")));
        activity.drawable_da = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#666666")));
        activity.drawable_xiao_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#ee5048")));
        activity.drawable_zhong_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#ee5048")));
        activity.drawable_da_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#ee5048")));
        activity.read_mode_n = PublicUtil.readBitMap(context, R.drawable.lk_reader_light_more);
        activity.zitijiaBitmap = PublicUtil.readBitMap(context, R.drawable.lk_reader_ajia);
        activity.zitijianBitmap = PublicUtil.readBitMap(context, R.drawable.lk_reader_ajian);
        changeColor(activity);
    }

    /*** 初始化背景的颜色和章节字颜色*/
    public static void setbg(int type,ReaderDetailActivity activity) {
        boolean isNighttime = SPUtil.getBoolean("model_flag", false);

        if (isNighttime) {
            activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg06);
            activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogressgay));
            activity.chapter_name.setTextColor(Color.parseColor("#CCCCCC"));
        }else {
            switch (type) {
                case 0:
                    activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg01);
                    activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogress));
                    activity.chapter_name.setTextColor(Color.parseColor("#333333"));
                    break;
                case 2:
                    activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg02);
                    activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogress));
//                    activity.chapter_name.setTextColor(Color.parseColor("#333333"));
                    break;
                case 4:
                    activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg03);
                    activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogress));
                    activity.chapter_name.setTextColor(Color.parseColor("#333333"));
                    break;
                case 12:
                    activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg04);
                    activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogressgay));
                    activity.chapter_name.setTextColor(Color.parseColor("#333333"));
                    break;
                case 14:
                    activity.lk_reader_bg_img.setBackgroundResource(R.drawable.readerdetail_bg05);
                    activity.progress.setIndeterminateDrawable(activity.getResources().getDrawable(R.drawable.readprogressgay));
                    activity.chapter_name.setTextColor(Color.parseColor("#333333"));
                    break;
            }
        }

    }

    /**
     * 0 表示夜间白天  1 第一个背景 2 第三个背景  3 第三个背景 依次到5
     */
    public static void changeColor(ReaderDetailActivity activity) {
        if (SPUtil.getBoolean("model_flag", false)) {  //夜间

            activity.back_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.back_b, Color.parseColor("#CCCCCC")));
            activity.buy_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.buy_b, Color.parseColor("#CCCCCC")));
            activity.huiying_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.huiying_b, Color.parseColor("#CCCCCC")));
            activity.dashang_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.dashang_b, Color.parseColor("#CCCCCC")));
            activity.more_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.more_b, Color.parseColor("#CCCCCC")));
            activity.ziti_jian.setBackgroundResource(R.drawable.radioborder);
            activity.ziti_jia.setBackgroundResource(R.drawable.radioborder);
            activity.czitijia = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.zitijiaBitmap, Color.parseColor("#999999")));
            activity.czitijian = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.zitijianBitmap, Color.parseColor("#999999")));
            activity.image_jia.setImageDrawable(activity.czitijia);
            activity.image_jian.setImageDrawable(activity.czitijian);
            activity.drawable_xiao = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#666666")));
            activity.drawable_zhong = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#666666")));
            activity.drawable_da = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#666666")));
            activity.drawable_xiao_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#cccccc")));
            activity.drawable_zhong_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#cccccc")));
            activity.drawable_da_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#cccccc")));
            activity.drawable_border = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_border, Color.parseColor("#666666")));
            activity.drawable_border_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_border, Color.parseColor("#cccccc")));
            activity.draw_mulu = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mulu, Color.parseColor("#999999")));
            activity.draw_font = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_font, Color.parseColor("#999999")));
            activity.draw_mode_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mode_r, Color.parseColor("#999999")));
            activity.draw_mode_n = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mode_n, Color.parseColor("#999999")));
            activity.plan_s_img.setTextColor(Color.parseColor("#999999"));
            activity.plan_x_img.setTextColor(Color.parseColor("#999999"));
            activity.rl_plan.setBackgroundColor(Color.parseColor("#111111"));
            activity.lin_top.setBackgroundColor(Color.parseColor("#111111"));
            activity.new_bottom_menu.setBackgroundColor(Color.parseColor("#111111"));
            activity.lin_setting.setBackgroundColor(Color.parseColor("#111111"));
            activity.seekBar1.setProgressDrawable(activity.getResources().getDrawable(R.drawable.seekbar_define_style));
            activity.seekBar1.setThumb(activity.getResources().getDrawable(R.drawable.seekbar_thumb_style));
            activity.seekBar2.setProgressDrawable(activity.getResources().getDrawable(R.drawable.seekbar_define_style));
            activity.seekBar2.setThumb(activity.getResources().getDrawable(R.drawable.seekbar_thumb_style));
            activity.fontSizeShow.setTextColor(Color.parseColor("#999999"));
            if (SPUtil.getBoolean("model_flag", false)) {
                activity.iv_reader_mode.setImageDrawable(activity.draw_mode_n);
            }else {
                activity.iv_reader_mode.setImageDrawable(activity.draw_mode_r);
            }
            activity.image_dashan.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.image_pinglun.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.lin_buy_bg.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.reader_tx_allday.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.reader_tx_setting.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.reader_tx_mulu.setTextColor(activity.getResources().getColor(R.color.C_CCCCCC));
            activity.rl_more.setBackgroundResource(R.drawable.checkbox_bg2);
            activity.tv_bookdes.setTextColor(Color.parseColor("#999999"));
            activity.cb_zdbuy.setTextColor(Color.parseColor("#999999"));
        }else {
            activity.back_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.back_b, Color.parseColor("#66ccdd")));
            activity.buy_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.buy_b, Color.parseColor("#FFFFFF")));
            activity.huiying_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.huiying_b, Color.parseColor("#66ccdd")));
            activity.dashang_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.dashang_b, Color.parseColor("#66ccdd")));
            activity.more_d = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.more_b, Color.parseColor("#66ccdd")));
            activity.ziti_jian.setBackgroundResource(R.drawable.radioborder_lan);
            activity.ziti_jia.setBackgroundResource(R.drawable.radioborder_lan);
            activity.drawable_xiao = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#cccccc")));
            activity.drawable_zhong = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#cccccc")));
            activity.drawable_da = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#cccccc")));
            activity.drawable_xiao_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_xiao, Color.parseColor("#66ccdd")));
            activity.drawable_zhong_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_zhong, Color.parseColor("#66ccdd")));
            activity.drawable_da_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_da, Color.parseColor("#66ccdd")));
            activity.drawable_border = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_border, Color.parseColor("#cccccc")));
            activity.drawable_border_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_border, Color.parseColor("#66ccdd")));
            activity.czitijia = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.zitijiaBitmap, Color.parseColor("#66ccdd")));
            activity.czitijian = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.zitijianBitmap, Color.parseColor("#66ccdd")));
            activity.image_jia.setImageDrawable(activity.czitijia);
            activity.image_jian.setImageDrawable(activity.czitijian);
            activity.draw_mulu = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mulu, Color.parseColor("#66ccdd")));
            activity.draw_font = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_font, Color.parseColor("#66ccdd")));
            activity.draw_mode_r = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mode_r, Color.parseColor("#66ccdd")));
            activity.draw_mode_n = new BitmapDrawable(PublicUtil.getAlphaBitmap(activity.read_mode_n, Color.parseColor("#66ccdd")));
            activity.plan_s_img.setTextColor(Color.parseColor("#66ccdd"));
            activity.plan_x_img.setTextColor(Color.parseColor("#66ccdd"));
            activity.lin_top.setBackgroundColor(Color.parseColor("#ffffff"));
            activity.rl_plan.setBackgroundColor(Color.parseColor("#ffffff"));
            activity.new_bottom_menu.setBackgroundColor(Color.parseColor("#ffffff"));
            activity.lin_setting.setBackgroundColor(Color.parseColor("#ffffff"));
            activity.seekBar1.setProgressDrawable(activity.getResources().getDrawable(R.drawable.seekbar_define_style_day));
            activity.seekBar1.setThumb(activity.getResources().getDrawable(R.drawable.seekbar_thumb_style_day));
            activity.seekBar2.setProgressDrawable(activity.getResources().getDrawable(R.drawable.seekbar_define_style_day));
            activity.seekBar2.setThumb(activity.getResources().getDrawable(R.drawable.seekbar_thumb_style_day));
            activity.fontSizeShow.setTextColor(Color.parseColor("#66ccdd"));
            activity.iv_reader_mode.setImageDrawable(activity.draw_mode_r);
            activity.rl_more.setBackgroundResource(R.drawable.checkbox_bg1);
            activity.tv_bookdes.setTextColor(Color.parseColor("#333333"));
            activity.cb_zdbuy.setTextColor(Color.parseColor("#333333"));
            activity.image_dashan.setTextColor(Color.parseColor("#66ccdd"));
            activity.image_pinglun.setTextColor(Color.parseColor("#66ccdd"));
            activity.lin_buy_bg.setTextColor(Color.parseColor("#66ccdd"));
            activity.reader_tx_allday.setTextColor(Color.parseColor("#66ccdd"));
            activity.reader_tx_setting.setTextColor(Color.parseColor("#66ccdd"));
            activity.reader_tx_mulu.setTextColor(Color.parseColor("#66ccdd"));
        }
        activity.back_tv.setImageDrawable(activity.back_d);

        activity.image_more.setImageDrawable(activity.more_d);
        activity.lin_top.getBackground().setAlpha(254);
        activity.lin_setting.getBackground().setAlpha(254);
        activity.new_bottom_menu.getBackground().setAlpha(254);
        activity.rl_plan.getBackground().setAlpha(254);
        activity.iv_mulu.setImageDrawable(activity.draw_mulu);
        activity.iv_font.setImageDrawable(activity.draw_font);
        setIntervalBtn(activity,SPUtil.getInt("content_interval", 2));
    }
    /**
     * 设置间距按钮颜色
     *
     * @param content_interval 1：最小间距 2：中等间距 3：最大间距
     */
    public static void setIntervalBtn(ReaderDetailActivity activity,int content_interval) {
        switch (content_interval) {
            case 1:
                activity.jianju_xiao_image.setImageDrawable(activity.drawable_xiao_r);
                activity.jianju_zhong_image.setImageDrawable(activity.drawable_zhong);
                activity.jianju_da_image.setImageDrawable(activity.drawable_da);
                activity.jianju_xiao.setBackgroundDrawable(activity.drawable_border_r);
                activity.jianju_zhong.setBackgroundDrawable(activity.drawable_border);
                activity.jianju_da.setBackgroundDrawable(activity.drawable_border);

                break;
            case 2:
                activity.jianju_xiao_image.setImageDrawable(activity.drawable_xiao);
                activity.jianju_zhong_image.setImageDrawable(activity.drawable_zhong_r);
                activity.jianju_da_image.setImageDrawable(activity.drawable_da);

                activity.jianju_xiao.setBackgroundDrawable(activity.drawable_border);
                activity.jianju_zhong.setBackgroundDrawable(activity.drawable_border_r);
                activity.jianju_da.setBackgroundDrawable(activity.drawable_border);
                break;
            case 3:
                activity.jianju_xiao_image.setImageDrawable(activity.drawable_xiao);
                activity.jianju_zhong_image.setImageDrawable(activity.drawable_zhong);
                activity.jianju_da_image.setImageDrawable(activity.drawable_da_r);

                activity.jianju_xiao.setBackgroundDrawable(activity.drawable_border);
                activity.jianju_zhong.setBackgroundDrawable(activity.drawable_border);
                activity.jianju_da.setBackgroundDrawable(activity.drawable_border_r);
                break;

        }
    }

}
